use sea_orm::{DatabaseConnection, DbErr, EntityTrait};
use crate::entities::user::{Entity as UserEntity, Model};
use log::{info, error};

/// user data access layer
pub struct UserRepository;

impl UserRepository {
    pub async fn find_all(db: &DatabaseConnection) -> Result<Vec<Model>, DbErr> {
        info!("Fetching all users from database");
        match UserEntity::find().all(db).await {
            Ok(users) => {
                info!("Successfully fetched {} users", users.len());
                Ok(users)
            }
            Err(e) => {
                error!("Failed to fetch users: {:?}", e);
                Err(e)
            }
        }
    }

    pub async fn delete_by_id(db: &DatabaseConnection, id: i64) -> Result<bool, DbErr> {
        info!("Deleting user with id: {}", id);
        let result = UserEntity::delete_by_id(id).exec(db).await?;
        info!("Delete result: {:?}", result);
        Ok(result.rows_affected > 0)
    }
}